﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Security.Cryptography;

namespace ChatRoom
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            txtIP.Text = "127.0.0.1";
            txtPort.Text = "8888";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string IP, name, pass;
            int port, level = 0;

            pass = txtPassword.Text;
            IP = txtIP.Text;
            port = int.Parse(txtPort.Text);
            name = txtUserName.Text;
            level = password(pass);

            frmChat chat = new frmChat(IP, port, name, level);
            this.Hide();
            chat.ShowDialog();
            this.Show();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Environment.Exit(Environment.ExitCode);
        }

        private void btnStartServer_Click(object sender, EventArgs e)
        {
            IPHostEntry host;
            String localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                localIP = ip.ToString();
            }
            MessageBox.Show("" + localIP);
            Thread serverThread = new Thread(startServer);
            serverThread.Start();
            
            string IP, name, pass;
            int port, level = 0;

            pass = txtPassword.Text;
            IP = txtIP.Text;
            port = int.Parse(txtPort.Text);
            name = txtUserName.Text;
            level = password(pass);
            
            frmChat chat = new frmChat(IP, port, name, level);
            this.Hide();
            chat.ShowDialog();
            this.Show();
        }
        private static void startServer()
        {
            Server.server();
        }

        private static int password(string pass)
        {
            string unencrypted = pass;
            byte[] encrypted;
            string encryptedtext;
            int level;

            string KEY = "9C-25-06-0E-77-81-87-56-1B-80-E2-81-12-C6-67-E6-70-AA-57-E2-41-2C-CD-D4-21-0F-78-63-1F-A2-59-3F-FB-5B-C6-AF-0C-CA-FF-D9-1F-E1-F7-40-0B-67-68-0A-E8-E7-D2-94-91-55-9B-0B-FE-76-B5-CB-9A-19-A5-5F";
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            UTF32Encoding utf8 = new UTF32Encoding();
            AesCryptoServiceProvider tDES = new AesCryptoServiceProvider();
            tDES.Key = md5.ComputeHash(utf8.GetBytes(KEY));
            tDES.Mode = CipherMode.ECB;
            tDES.Padding = PaddingMode.PKCS7;
            ICryptoTransform trans = tDES.CreateEncryptor();
            encrypted = trans.TransformFinalBlock(utf8.GetBytes(unencrypted), 0, utf8.GetBytes(unencrypted).Length);
            encryptedtext = BitConverter.ToString(encrypted);

            StreamReader sr = new StreamReader("Admins.txt");
            if (encryptedtext == sr.ReadLine() || encryptedtext == sr.ReadLine() || encryptedtext == sr.ReadLine())
            {
                level = 1;
            }
            else
            {
                level = 0;
            }
            return level;
        }
    }
}
